//
//  LinkViewController.m
//  TableView 封装
//
//  Created by Kevin on 17/11/1.
//  Copyright © 2017年 JOWO.team. All rights reserved.
//

#import "LinkViewController.h"
#import "KV_TableView.h"
#import "TestCell.h"
#import "TestModel.h"
#import "JCAlertView.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
@interface LinkViewController ()

@property (nonatomic, strong) KV_TableView *oneTableView;
@property (nonatomic, strong) KV_TableView *twoTableView;

@property (nonatomic, copy) NSMutableArray *oneDataArray;
@property (nonatomic, copy) NSMutableArray *twoDataArray;

@property (nonatomic, copy) NSString *name;
@end

@implementation LinkViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 设置为 NO后系统会让你负责ScrollView自动布局
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _oneDataArray = [NSMutableArray array];
    _twoDataArray = [NSMutableArray array];
    
    self.oneTableView = [[KV_TableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self.view addSubview:self.oneTableView];
    
    self.twoTableView = [[KV_TableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/3, 64, (SCREEN_WIDTH/3)*2, SCREEN_HEIGHT)];
    [self.view addSubview:self.twoTableView];

    
    _name = @"右侧的";
    self.oneTableView.cellHeight = 50;
    self.twoTableView.cellHeight = 60;
    
    [self loadData];
    [self loadData1];
    
    
    [self.oneTableView setUpTheDataSourceWithArray:_oneDataArray];
    [self.twoTableView setUpTheDataSourceWithArray:_twoDataArray];
    
    
    // ** __weak LinkViewController *weakSelf = self;
    // ** 下面存在自引用建议使用weakSelf，理由已经在NormalController中表明
    
    // 第一个 TableView点击 刷新第二个视图
    [self.oneTableView clickCellWithBlock:^(NSIndexPath *index) {
            NSString *str = [NSString stringWithFormat:@"你点击了第%ld条",index.row];
            _name = [NSString stringWithFormat:@"第%ld条",index.row];
            [JCAlertView showOneButtonWithTitle:str Message:@"需要刷新么?" ButtonType:0 ButtonTitle:@"确认" Click:^{
                // 点击 cell 刷新 另一个  TableView 的视图(实现联动)
                [self loadData1];
            }];
           
        }];
        
  
    [self.twoTableView clickCellWithBlock:^(NSIndexPath *index) {
        
    }];

}
- (void)loadData1{
    
    [_twoDataArray removeAllObjects];
    for (int i = 0; i < 20; i++) {
        NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:0];
        TestModel *model   = [TestModel new];
        model.title        = _name;
        TestCell *cell     = [TestCell creatXibCellWithTarget:self index:index tableView:self.oneTableView model:model];
        
        cell.backgroundColor = [UIColor orangeColor];

        [_twoDataArray addObject:cell];

    }
  
    NSLog(@"视图刷新了");
    [self.twoTableView setUpTheDataSourceWithArray:_twoDataArray];
}

- (void)loadData{
    
    
    for (int i = 0; i < 20; i++) {
        static NSString *identifer = @"1111";
        UITableViewCell *cell = [self.twoTableView dequeueReusableCellWithIdentifier:identifer];
        
        if (!cell) {
            cell = [(UITableViewCell *)[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"1111"];
        }
 
        cell.backgroundColor = [UIColor grayColor];
        
        cell.textLabel.text = [NSString stringWithFormat:@"左侧的第%d个",i];
        [_oneDataArray addObject:cell];
    }
    
}

- (void)dealloc {
    NSLog(@"%s",__func__);
}


@end
